package net.tensor.tensorprovider.controller;

import net.tensor.tensorprovider.exception.ResultBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
public class UploadController {
    private static Logger logger = LoggerFactory.getLogger(UploadController.class);

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public ResultBody upload(@RequestParam("file") List<MultipartFile> files) throws IOException {
        logger.info("开始执行upload方法");
        if (files.isEmpty()) {
            return ResultBody.error("文件不存在");
        }

        logger.info("初始化OSSClient");
        for (MultipartFile file : files
        ) {
            logger.info("开始上传文件====>" + file.getOriginalFilename());
            String path = "/home/q/tmp/upFiles/";
            File dir = new File(path);
            if (!dir.exists()) dir.mkdirs();
            File file1 = new File(path + UUID.randomUUID().toString().replace("-", "") + file.getOriginalFilename());
            file.transferTo(file1);
        }
        return ResultBody.success("成功");
    }
}
